var mysql = require('mysql');
var config = require('../conf/db');
var $sql = require('../sql/userSql');

var pool = mysql.createPool( config.mysql );

module.exports = {
    queryTokenByUsernameAndPassword: function (req, res, next) {
        pool.getConnection(function(err, connection) {
            var params = req.body;
            console.log(params);
            connection.query($sql.queryTokenByUsernameAndPassword, [params.username, params.password], function(err, result) {
                if(err) {
                    res.send(err); 
                }else{
                    if(result[0]){
                        res.send({
                            status:200,
                            data:result[0]
                        });
                    }else{
                        res.send({
                            status:0,
                            data:{
                                message:"用户名密码错误"
                            }
                        });
                    }
                    
                }
 
                // 释放连接 
                connection.release();
            });
        });
    },
    queryMenuByUserId:function(req,res,next){
        pool.getConnection(function(err, connection) {
            var params = req.body;
            console.log(params);
            connection.query($sql.queryMenuByUserId, [params.id], function(err, result) {
                if(err) {
                    res.send(err); 
                }else{
                    res.send({
                        status:200,
                        data:{
                            menu:result
                        }
                    });
                }
 
                // 释放连接 
                connection.release();
            });
        });
    }
};